package org.example.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.hzit.UserVo.UserVo;
import org.example.hzit.entity.User;
import org.example.hzit.mapper.UserMapper;
import org.example.hzit.repository.UserRepository;
import org.example.hzit.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.hzit.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author gww
 * @since 2025-01-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public PageResult<User> findByPage(Integer page, Integer pageSize) {
        Page<User> page1=this.page(new Page<>(page,pageSize));

        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public PageResult<User> search(Integer page, Integer pageSize, UserVo vo) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getUsername()),"username",vo.getUsername());
        wrapper.like(StrUtil.isNotBlank(vo.getPhone()),"phone",vo.getPhone());
        Page<User>userPage = this.page(new Page<>(page, pageSize), wrapper);

        return new  PageResult<>(userPage.getRecords(),userPage.getTotal());
    }

    @Override
    public User addUser(User user){
        return userRepository.save(user);
    }
}
